package com.example.servlet;

import com.example.dao.NovelDao;
import com.example.domain.Chapter;
import com.example.domain.Novel;
import com.example.domain.User;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@WebServlet("/NovelDetailServlet")
public class NovelDetailServlet extends HttpServlet{
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
    {
        request.setCharacterEncoding("utf-8");
        int novel_id=Integer.parseInt(request.getParameter("novel_id"));
//        User user= (User) request.getSession().getAttribute("user");
//        int user_id=user.getUser_id();
        NovelDao novelDao=new NovelDao();
        Novel novel=novelDao.getNovelById(novel_id);
        int affteredRow = novelDao.updateClickCount(novel_id);
        if (affteredRow==0){
            request.setAttribute("msg","未增加点击量！");
        }
        HttpSession session = request.getSession();
        if(novel==null){
            request.setAttribute("msg","该小说不存在");
            request.getRequestDispatcher("index.jsp").forward(request,response);
            return;
        }
        List<Chapter> chapterList=novelDao.getChapterListAndContent(novel_id);
        if(chapterList.isEmpty()){
            request.setAttribute("msg","获取小说章节失败");
            request.getRequestDispatcher("index.jsp").forward(request,response);
            return;
        }
        String lastTitle=chapterList.get(chapterList.size()-1).getTitle();
        session.setAttribute("novel", novel);
        session.setAttribute("chapterList", chapterList);
        request.setAttribute("lastTitle", lastTitle);
        request.getRequestDispatcher("novelDetail.jsp").forward(request,response);
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
    {
        doGet(request,response);
    }
}
